package com.mfangsoft.zhuangjialong.integration.user.controller;

import java.net.URI;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mfangsoft.zhuangjialong.common.context.UserContext;
import com.mfangsoft.zhuangjialong.common.model.Page;
import com.mfangsoft.zhuangjialong.common.model.ResponseMessage;
import com.mfangsoft.zhuangjialong.common.utils.HttpClientUtil;
import com.mfangsoft.zhuangjialong.common.utils.RedisUtils;
import com.mfangsoft.zhuangjialong.common.utils.SysConstant;
import com.mfangsoft.zhuangjialong.integration.enums.UserType;
import com.mfangsoft.zhuangjialong.integration.user.model.UserEntity;
import com.mfangsoft.zhuangjialong.integration.user.service.UserService;
import com.mongodb.util.JSON;

import cn.jpush.api.report.UsersResult.User;

@Controller(value="user")
@RequestMapping("/server")
public class UserController {

	@Autowired
	private UserService userService; 
//	/**
//	 * 创建用户
//	 * @param user
//	 * @return
//	 */
	@RequestMapping(value="/createuser",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> createUser(@RequestBody UserEntity userEntity) {
		
		ResponseMessage<String> message = new ResponseMessage<String>();
		
		try {
			return userService.addUser(userEntity);
		} catch (Exception e) {
			
			message.setCode(SysConstant.FAILURE_CODE);
	        message.setMessage(SysConstant.FAILURE_MSG);
	        return message;
			// TODO: handle exception
		}
	}
	
	@RequestMapping(value="/modifyuser",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> modifyUser(@RequestBody UserEntity userEntity) {
		
		ResponseMessage<String> message = new ResponseMessage<String>();
		
		try {
			return userService.modifyUser(userEntity);
		} catch (Exception e) {
			
			message.setCode(SysConstant.FAILURE_CODE);
	        message.setMessage(SysConstant.FAILURE_MSG);
	        return message;
			// TODO: handle exception
		}
		
		
		
	}
	
	@RequestMapping(value="/getuser/{id}",method=RequestMethod.GET)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<UserEntity> modifyUser(@PathVariable Long  id) {
		
		ResponseMessage<UserEntity> message = new ResponseMessage<UserEntity>();
		
		try {
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			message.setResult(userService.getUserInfoById(id));
		} catch (Exception e) {
			
			message.setCode(SysConstant.FAILURE_CODE);
//	        message.setMessage(SysConstant.FAILURE_MSG);
			// TODO: handle exception
		}
		
		
		return message;
		
	}
	/**
	 * 停用账号
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/update/{type}/{id}/{value}",method=RequestMethod.GET)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> closeUser(@PathVariable Long id,@PathVariable Integer type,@PathVariable Integer value) {
		
		ResponseMessage<String> message = new ResponseMessage<String>();
		
		
		try {
			
			
			 if(type.intValue()==1){
				 
				 userService.updateUserState(id, value);
			 }else{
				 userService.updateUserUpAndDown(id, value);
			 }
			
			
			
				
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
			
		} catch (Exception e) {
			
			 message.setCode(SysConstant.FAILURE_CODE);
		     message.setMessage(e.getMessage());
			// TODO: handle exception
		}
		return message;
		
	}
//	
	
//	
//	@RequestMapping(value="/modifypassword",method=RequestMethod.POST)
//	@ResponseBody
//	@CrossOrigin
//	public ResponseMessage<String> modifyPassword(@RequestBody Map<String,Object> map) {
//		
//		ResponseMessage<String> message = new ResponseMessage<String>();
//		
//		if(map.get("id")==null){
//			
//			 message.setCode(SysConstant.FAILURE_CODE);
//		     message.setMessage(SysConstant.FAILURE_MSG);
//		     return message;
//		}
//		
//		if(map.get("old_pw")==null){
//			
//			 message.setCode(SysConstant.FAILURE_CODE);
//		     message.setMessage(SysConstant.FAILURE_MSG);
//		     return message;
//		}
//		
//		if(map.get("new_pw")==null){
//			
//			 message.setCode(SysConstant.FAILURE_CODE);
//		     message.setMessage(SysConstant.FAILURE_MSG);
//		     return message;
//		}
//		
//		
//		try {
//			if(userService.modifyPassword(new Long(map.get("id").toString()), map.get("old_pw").toString(), map.get("new_pw").toString(), message)){
//				message.setCode(SysConstant.SUCCESS_CODE);
//				message.setMessage(SysConstant.SUCCESS_MSG);
//			}
//		} catch (Exception e) {
//			
//			 message.setCode(SysConstant.FAILURE_CODE);
//		     message.setMessage(e.getMessage());
//			// TODO: handle exception
//		}
//		
//		return message;
//	}
////	
	@RequestMapping(value="/queryuserlist",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public Page<Map<String,Object>>  queryUserList(@RequestBody Page<Map<String,Object>> page) {
		
		
		
		
		
		return userService.queryUserList(page);
	}
//	@RequestMapping(value="/getUserType",method=RequestMethod.GET)
//	@ResponseBody
//	@CrossOrigin
//	public  ResponseMessage<Map<String,Object>> getUserType()
//	{
//		
//		Map<String,Object> map = new HashMap<>();
//		
//		for (UserType userType : UserType.values()) {
//			
//			map.put(userType.getName(), userType.getIndex());
//			userType.name();
//		}
//		ResponseMessage<Map<String,Object>> message = new ResponseMessage<>();
//		
//		message.setResult(map);
//		return message;
//	
//	}
//	
	@RequestMapping(value="/userbyid/{id}",method=RequestMethod.GET)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<UserEntity>  getUserById(@PathVariable Long id)
	{
		ResponseMessage<UserEntity>  message= new  ResponseMessage<>();
			
		UserEntity userEntity=userService.getUserById(id);
			if(userEntity!=null&& userEntity.getId()!=null){
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
				message.setResult(userEntity);
			}
		
		return message;
	
	}
//	
	@RequestMapping(value="/checkuser",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<UserEntity>  getCheckUser(@RequestBody Map<String,Object> map)
	{
		ResponseMessage<UserEntity>  message= new  ResponseMessage<>();
			
		
		try{
			
			
			
			UserEntity userEntity=userService.getUserByAccount(map.get("account").toString());
			
			if(userEntity!=null){
				
				message.setCode(SysConstant.FAILURE_CODE);
				message.setMessage(SysConstant.FAILURE_MSG);
			}else{
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
			}
		}catch (Exception e) {
			
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			// TODO: handle exception
		}
		
		
		return message;
	
	}
//	
	@RequestMapping(value="/resetpassword/{id}",method=RequestMethod.GET)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String>  getCheckUser(@PathVariable Long id)
	{
		ResponseMessage<String>  message= new  ResponseMessage<>();
			
		
		
		if(userService.resetpassword(id)){
			
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			
		}else{
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		
		return message;
	
	}
//	
//	
	@RequestMapping(value="/getpermissionall",method=RequestMethod.GET)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<List<Map<String,Object>>>  getPermissionALL()
	{
		ResponseMessage<List<Map<String,Object>>>  message= new  ResponseMessage<>();
			
		message.setCode(SysConstant.SUCCESS_CODE);
		message.setMessage(SysConstant.SUCCESS_MSG);
		message.setResult(userService.getPermissionALL());
		
		UserEntity  entity=UserContext.getCurrentUser();
		return message;
	
	}
//	
//	@RequestMapping(value="/getShopBgImg",method=RequestMethod.GET)
//	@ResponseBody
//	@CrossOrigin
//	public ResponseMessage<String>  getShopBgImg(){
//		ResponseMessage<String>  message= new  ResponseMessage<>();
//		message.setCode(SysConstant.SUCCESS_CODE);
//		message.setMessage(SysConstant.SUCCESS_MSG);
//		message.setResult(RedisUtils.getValue(SysConstant.SHOP_BG_NAME+UserContext.getCurrentUserId()));
//		return message;
//	
//	}
	
	/**
	 * 创建商户查询商户下所有下级商户的标签
	 * @return
	 */
	@RequestMapping(value="/selectShopTag",method=RequestMethod.GET)
	@ResponseBody
	@CrossOrigin
	public List<String> selectShopTag(){
		return userService.selectShopTag();
	}
	
	@RequestMapping(value="/selectZzhInfo",method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage<Map<String,Object>> selectZzhInfo(String zzh){
		ResponseMessage<Map<String,Object>> message= new  ResponseMessage<>();
		try {
			Map map = this.getSellerZZResult(zzh);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			if(map!=null){
				Map<String,Object> m=(Map<String, Object>) map.get("data");
				message.setResult(m);
			}else{
				message.setResult(map);
			}
		} catch (Exception e) {
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	@RequestMapping(value="/selectBmhInfo",method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage<Map<String,Object>> selectBmhInfo(String zzh,String bmh){
		ResponseMessage<Map<String,Object>> message= new  ResponseMessage<>();
		try {
			Map map = this.getSellerZZBMResult(zzh,bmh);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			if(map!=null){
				Map<String,Object> m=(Map<String, Object>) map.get("data");
				message.setResult(m);
			}else{
				message.setResult(map);
			}
		} catch (Exception e) {
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	
	public Map getSellerZZResult(String orgNo) {
		try {
			URI uri = new URI("http://mos.powerdekor.com.cn:8011/qobinf.ashx?type=GOI02&sn=qobi01.powerdekor.com.cn&orgNo="+ orgNo);
			String sellerLoginJson = HttpClientUtil.sendGetForJson(uri);
			System.out.println(sellerLoginJson);
			Map map = (Map)JSON.parse(sellerLoginJson);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return new HashMap();
		}
	}
	public Map getSellerZZBMResult(String orgNo,String depNo) {
		try {
			URI uri = new URI("http://mos.powerdekor.com.cn:8011/qobinf.ashx?type=GOI03&sn=qobi01.powerdekor.com.cn&orgNo="+ orgNo +"&depNo=" + depNo);
			String sellerLoginJson = HttpClientUtil.sendGetForJson(uri);
			System.out.println(sellerLoginJson);
			Map map = (Map)JSON.parse(sellerLoginJson);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return new HashMap();
		}
	}
}
